use crate::rtmp::messages::define::msg_type_id;
use crate::rtmp::user_control_messages::define;
use crate::rtmp::user_control_messages::errors::EventMessagesError;
use byteorder::BigEndian;
use bytesio::bytes_writer::AsyncBytesWriter;

// 写入事件结构体
pub struct EventMessagesWriter {
    writer: AsyncBytesWriter,
}

impl EventMessagesWriter {
    pub fn new(writer: AsyncBytesWriter) -> Self {
        Self { writer }
    }

    // 写入控制消息头
    fn write_control_message_header(&mut self, length: u32) -> Result<(), EventMessagesError> {
        /*
        0 1 2 3 4 5 6 7
        +-+-+-+-+-+-+-+-+
        |fmt|  cs id  |
        +-+-+-+-+-+-+-+-+
        0x0     0x02
        */
        // 写入格式和CS ID
        self.writer.write_u8(0x02)?; // fmt为0，csid为2
                                     // 写入时间戳，3字节，值为0
        self.writer.write_u24::<BigEndian>(0)?;
        // 写入消息长度
        self.writer.write_u24::<BigEndian>(length)?;
        // 写入消息类型ID
        self.writer.write_u8(msg_type_id::USER_CONTROL_EVENT)?;
        // 写入消息流ID，值为0
        self.writer.write_u32::<BigEndian>(0)?;

        Ok(())
    }

    // 异步写入流开始消息
    pub async fn write_stream_begin(&mut self, stream_id: u32) -> Result<(), EventMessagesError> {
        self.write_control_message_header(6)?;
        // 写入流开始事件类型
        self.writer
            .write_u16::<BigEndian>(define::RTMP_EVENT_STREAM_BEGIN)?;
        // 写入流ID
        self.writer.write_u32::<BigEndian>(stream_id)?;

        self.writer.flush().await?;
        Ok(())
    }

    // 异步流写入结束消息
    pub async fn write_stream_end(&mut self, stream_id: u32) -> Result<(), EventMessagesError> {
        self.write_control_message_header(6)?;
        // 写入流结束事件类型
        self.writer
            .write_u16::<BigEndian>(define::RTMP_EVENT_STREAM_END)?;
        // 写入流ID
        self.writer.write_u32::<BigEndian>(stream_id)?;

        self.writer.flush().await?;
        Ok(())
    }

    // 异步写入干燥流消息
    pub async fn write_stream_dry(&mut self, stream_id: u32) -> Result<(), EventMessagesError> {
        self.write_control_message_header(6)?;
        // 写入流干燥事件类型
        self.writer
            .write_u16::<BigEndian>(define::RTMP_EVENT_STREAM_DRY)?;
        // 写入流ID
        self.writer.write_u32::<BigEndian>(stream_id)?;

        self.writer.flush().await?;
        Ok(())
    }

    // 异步写入设置缓冲区长度消息
    pub async fn write_set_buffer_length(
        &mut self,
        stream_id: u32,
        ms: u32,
    ) -> Result<(), EventMessagesError> {
        self.write_control_message_header(10)?;
        // 写入设置缓冲长度事件类型
        self.writer
            .write_u16::<BigEndian>(define::RTMP_EVENT_SET_BUFFER_LENGTH)?;
        // 写入流ID
        self.writer.write_u32::<BigEndian>(stream_id)?;
        // 写入毫秒数
        self.writer.write_u32::<BigEndian>(ms)?;

        self.writer.flush().await?;
        Ok(())
    }

    // 异步写入流是否被记录消息
    pub async fn write_stream_is_record(
        &mut self,
        stream_id: u32,
    ) -> Result<(), EventMessagesError> {
        self.write_control_message_header(6)?;
        // 写入控制消息事件类型
        self.writer
            .write_u16::<BigEndian>(define::RTMP_EVENT_STREAM_IS_RECORDED)?;
        // 写入流ID
        self.writer.write_u32::<BigEndian>(stream_id)?;

        self.writer.flush().await?;

        Ok(())
    }

    // 异步写入Ping请求消息
    pub async fn write_ping_request(&mut self, timestamp: u32) -> Result<(), EventMessagesError> {
        self.write_control_message_header(6)?;
        // 写入Ping事件类型
        self.writer
            .write_u16::<BigEndian>(define::RTMP_EVENT_PING)?;
        // 写入时间戳
        self.writer.write_u32::<BigEndian>(timestamp)?;

        self.writer.flush().await?;
        Ok(())
    }

    // 异步写入Ping响应消息
    pub async fn write_ping_response(&mut self, timestamp: u32) -> Result<(), EventMessagesError> {
        self.write_control_message_header(6)?;
        // 写入Pong事件类型
        self.writer
            .write_u16::<BigEndian>(define::RTMP_EVENT_PONG)?;
        // 写入时间戳
        self.writer.write_u32::<BigEndian>(timestamp)?;

        self.writer.flush().await?;
        Ok(())
    }
}
